package com.tianyang.yukuaifuzs.business.auth.view;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.business.auth.bean.CompanyStoreInput;
import com.tianyang.yukuaifuzs.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：企业认证店铺信息
 * Created by xyj on 2024/3/10
 */
public class AuthCompanyStoreView extends LinearLayout {

    @BindView(R.id.shopHeadImgSdv)
    SimpleDraweeView shopHeadImgSdv;
    @BindView(R.id.shopOnImgSdv)
    SimpleDraweeView shopOnImgSdv;
    @BindView(R.id.shopDeskImgSdv)
    SimpleDraweeView shopDeskImgSdv;
    @BindView(R.id.shopOutImgSdv)
    SimpleDraweeView shopOutImgSdv;
    @BindView(R.id.shopNameEt)
    EditText shopNameEt;
    @BindView(R.id.shopUserNameEt)
    EditText shopUserNameEt;
    @BindView(R.id.shopPhoneEt)
    EditText shopPhoneEt;
    @BindView(R.id.shopProvincesAndCityTv)
    TextView shopProvincesAndCityTv;
    @BindView(R.id.shopAddressEt)
    EditText shopAddressEt;
    @BindView(R.id.confirmTv)
    Button confirmTv;

    @BindView(R.id.otherAttachmentLl)
    LinearLayout otherAttachmentLl;
    @BindView(R.id.otherAttachmentImgSdv)
    SimpleDraweeView otherAttachmentImgSdv;
    @BindView(R.id.otherAttachmentTitleTv)
    TextView otherAttachmentTitleTv;

    private SampleImageDialog vSampleImageDialog;

    private CompanyStoreInput mCompanyStoreInput;

    private SampleImageOutput mOutput;

    public AuthCompanyStoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_company_store, this);
        ButterKnife.bind(this);
        mCompanyStoreInput = new CompanyStoreInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.takeShopHeadTv, R.id.takeShopOnTv, R.id.takeShopDeskTv, R.id.takeShopOutTv, R.id.otherAttachmentTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.takeShopHeadTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopHeadPic());
                }
                break;
            case R.id.takeShopOnTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopOnPic());
                }
                break;
            case R.id.takeShopDeskTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopCashPic());
                }
                break;
            case R.id.takeShopOutTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopOutPic());
                }
                break;
            case R.id.otherAttachmentTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShopLocationPic());
                }
                break;
        }
    }

    private void showSampleImageDialog(SampleImageOutput.SampleImage sampleImage) {
        if (vSampleImageDialog == null) {
            vSampleImageDialog = new SampleImageDialog(getContext());
        }
        vSampleImageDialog.showPictureDataView(sampleImage);
        vSampleImageDialog.show();
    }

    public void onFeedBackView(CompanyStoreInput input, String posChannel) {
        if (TextUtils.equals(AuthConfig.POS_CHANNEL_YS, posChannel)
                || TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
            otherAttachmentLl.setVisibility(View.VISIBLE);
            if (TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
                otherAttachmentTitleTv.setText("其它图片");
            }
        } else {
            otherAttachmentLl.setVisibility(View.GONE);
        }
        if (input != null) {
            mCompanyStoreInput = input;
            if (!TextUtils.isEmpty(mCompanyStoreInput.getShopHeadPic()))
                shopHeadImgSdv.setImageURI(Uri.parse(mCompanyStoreInput.getShopHeadPic()));
            if (!TextUtils.isEmpty(mCompanyStoreInput.getShopOnPic()))
                shopOnImgSdv.setImageURI(Uri.parse(mCompanyStoreInput.getShopOnPic()));
            if (!TextUtils.isEmpty(mCompanyStoreInput.getShopCashPic()))
                shopDeskImgSdv.setImageURI(Uri.parse(mCompanyStoreInput.getShopCashPic()));
            if (!TextUtils.isEmpty(mCompanyStoreInput.getShopOutPic()))
                shopOutImgSdv.setImageURI(Uri.parse(mCompanyStoreInput.getShopOutPic()));
            if (!TextUtils.isEmpty(mCompanyStoreInput.getOtherAttachment()))
                otherAttachmentImgSdv.setImageURI(Uri.parse(mCompanyStoreInput.getOtherAttachment()));
            shopNameEt.setText(VerifyUtil.checkString(mCompanyStoreInput.getShopName()));
            shopUserNameEt.setText(VerifyUtil.checkString(mCompanyStoreInput.getShopUserName()));
            shopPhoneEt.setText(VerifyUtil.checkString(mCompanyStoreInput.getShopPhone()));
            if (!TextUtils.isEmpty(mCompanyStoreInput.getShopProvinceName())) {
                String placeCodeText = VerifyUtil.checkString(mCompanyStoreInput.getShopProvinceName())
                        + " " + VerifyUtil.checkString(mCompanyStoreInput.getShopCityName())
                        + " " + VerifyUtil.checkString(mCompanyStoreInput.getShopAreaName());
                shopProvincesAndCityTv.setText(placeCodeText);
            }
            shopAddressEt.setText(VerifyUtil.checkString(mCompanyStoreInput.getShopAddress()));
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_SHOPHEAD:
                shopHeadImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyStoreInput.setShopHeadPic(urlStr);
                break;
            case CommonConfig.TYPE_SHOPON:
                shopOnImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyStoreInput.setShopOnPic(urlStr);
                break;
            case CommonConfig.TYPE_SHOPDESK:
                shopDeskImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyStoreInput.setShopCashPic(urlStr);
                break;
            case CommonConfig.TYPE_SHOPOUT:
                shopOutImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyStoreInput.setShopOutPic(urlStr);
                break;
            case CommonConfig.TYPE_OTHER_ATTACHMENT:
                otherAttachmentImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyStoreInput.setOtherAttachment(urlStr);
                break;
        }
    }

    public void showProvCityAreaView(String provCityName, String wheelCode) {
        if (!TextUtils.isEmpty(provCityName)) {
            String[] provCity = provCityName.split(" ");
            mCompanyStoreInput.setShopProvinceName(provCity[0]);
            mCompanyStoreInput.setShopCityName(provCity[1]);
            if (provCity.length > 2)
                mCompanyStoreInput.setShopAreaName(provCity[2]);
            String[] wheelCodes = wheelCode.split(" ");
            mCompanyStoreInput.setShopProvinceCode(wheelCodes[0]);
            mCompanyStoreInput.setShopCityCode(wheelCodes[1]);
            if (wheelCodes.length > 2)
                mCompanyStoreInput.setShopAreaCode(wheelCodes[2]);
        }
    }

    public CompanyStoreInput getCompanyStoreInput() {
        mCompanyStoreInput.setShopName(shopNameEt.getText().toString());
        mCompanyStoreInput.setShopUserName(shopUserNameEt.getText().toString());
        mCompanyStoreInput.setShopPhone(shopPhoneEt.getText().toString());
        mCompanyStoreInput.setShopAddress(shopAddressEt.getText().toString());
        return mCompanyStoreInput;
    }

    public boolean checkCompanyStore() {
        if (TextUtils.isEmpty(mCompanyStoreInput.getShopHeadPic())) {
            ToastUtil.show("请上传门头照");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyStoreInput.getShopOnPic())) {
            ToastUtil.show("请上传店内环境照");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyStoreInput.getShopCashPic())) {
            ToastUtil.show("请上传收银台照");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyStoreInput.getShopOutPic())) {
            ToastUtil.show("请上传门牌照（店外环境照）");
            return false;
        }
        if (TextUtils.isEmpty(shopNameEt.getText())) {
            ToastUtil.show("请填写门店全称");
            SoftInputUtil.openKeybord(shopNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(shopUserNameEt.getText())) {
            ToastUtil.show("请输入门店联系人");
            SoftInputUtil.openKeybord(shopUserNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(shopPhoneEt.getText())) {
            ToastUtil.show("请填写联系人手机号");
            SoftInputUtil.openKeybord(shopPhoneEt, getContext());
            return false;
        } else if (VerifyUtil.checkPhoneNumber(shopPhoneEt.getText())) {
            ToastUtil.show("请输入正确的手机号");
            SoftInputUtil.openKeybord(shopPhoneEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mCompanyStoreInput.getShopProvinceCode())) {
            ToastUtil.show("请选择门店所在省市");
            return false;
        }
        if (TextUtils.isEmpty(shopAddressEt.getText())) {
            ToastUtil.show("请填写店铺详细地址");
            SoftInputUtil.openKeybord(shopAddressEt, getContext());
            return false;
        }
        return true;
    }

    public void setOnlyReadView() {
        shopNameEt.setEnabled(false);
        shopUserNameEt.setEnabled(false);
        shopPhoneEt.setEnabled(false);
        shopProvincesAndCityTv.setEnabled(false);
        shopProvincesAndCityTv.setCompoundDrawables(null, null, null, null);
        shopAddressEt.setEnabled(false);
        confirmTv.setVisibility(GONE);
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
    }
}
